package com.example.filedemo;

import android.os.Environment;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 文件读取工具类
 */
public class FileUtil {
    private static SimpleDateFormat sdf = new SimpleDateFormat("HHmmssSSS", Locale.CHINA);

    /**
     * 读取文件内容，作为字符串返回
     */
    public static String readFileAsString(String filePath) throws IOException {
        File file = new File(filePath);
        if (!file.exists()) {
            throw new FileNotFoundException(filePath);
        }

        if (file.length() > 1024 * 1024 * 1024) {
            throw new IOException("File is too large");
        }

        StringBuilder sb = new StringBuilder((int) (file.length()));
        // 创建字节输入流  
        FileInputStream fis = new FileInputStream(filePath);
        // 创建一个长度为10240的Buffer
        byte[] bbuf = new byte[10240];
        // 用于保存实际读取的字节数  
        int hasRead = 0;
        while ((hasRead = fis.read(bbuf)) > 0) {
            sb.append(new String(bbuf, 0, hasRead));
        }
        fis.close();
        return sb.toString();
    }

    /**
     * 百度AI转为流
     * 根据文件路径读取byte[] 数组
     */
    public static byte[] readFileByBytes(String filePath) throws IOException {
        File file = new File(filePath);
        if (!file.exists()) {
            throw new FileNotFoundException(filePath);
        } else {
            ByteArrayOutputStream bos = new ByteArrayOutputStream((int) file.length());
            BufferedInputStream in = null;

            try {
                in = new BufferedInputStream(new FileInputStream(file));
                short bufSize = 1024;
                byte[] buffer = new byte[bufSize];
                int len1;
                while (-1 != (len1 = in.read(buffer, 0, bufSize))) {
                    bos.write(buffer, 0, len1);
                }

                byte[] var7 = bos.toByteArray();
                return var7;
            } finally {
                try {
                    if (in != null) {
                        in.close();
                    }
                } catch (IOException var14) {
                    var14.printStackTrace();
                }

                bos.close();
            }
        }
    }

    /**
     * 创建APK的保存路径
     */
    public static File createFileDar() {
        String apkDir = "";
        //* @Description:判断是否插入SD卡
        boolean isHaveSdCard = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
        /* 创建路径的时候一定要用[/],不能使用[\]*/
        if (isHaveSdCard) {
            apkDir = Aiconstant.APP_ROOT_PATH;// 保存到SD卡路径下
        } else {
            apkDir = MyApplication.getContext().getFilesDir().getAbsolutePath() + "/aiface/";// 保存到app的包名路径下
        }

        File file = new File(apkDir);
        if (!file.exists()) {
            boolean boo = file.mkdirs();
        }
        return file;
    }

    /**
     * 保存人头路径
     *
     * @param bitmapByte   人脸流
     * @param fileCompress 压缩文件
     */
    public static String saveHeadPic(byte[] bitmapByte, File fileCompress) {
        String fileName = fileCompress.getName().substring(0, fileCompress.getName().indexOf(".")) + sdf.format(new Date()) + ".png";
        String filePath = Aiconstant.APP_ROOT_PATH + File.separator + fileName;
        DebugLog.d(DebugLog.LOGHsc + " saveHeadPic--- filePath " + filePath);
        File filePng = new File(filePath);
        try {
            getFilesRead(filePng, bitmapByte);
        } catch (IOException e) {
            DebugLog.d(DebugLog.LOGHsc + " e --saveHeadPic " + e.toString());
            e.printStackTrace();
        }
        return filePath;
    }

    /**
     * 獨流
     *
     * @param file
     * @param mData
     * @throws IOException
     */
    public static void getFilesRead(File file, byte[] mData) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(mData, 0, mData.length));
        FileOutputStream fos = new FileOutputStream(file);
        byte[] buff = new byte[8192];
        int len;
        while ((len = bis.read(buff)) != -1) {
            fos.write(buff, 0, len);
        }
        fos.flush();
        fos.close();
        bis.close();

    }
}
